 #include <mainwindow.h>
#include <ctime>
#include <QDebug>
#include <QMessageBox>
using namespace std;
void MainWindow::randomSet(){
    if(end_game()) return;
}

void MainWindow::c_vs_cmode(){
    randomSet();
}

void MainWindow::c_vs_hmode(){
    repaint();
    if(isfirst) {
        setChess(Chess(7,10).i,Chess(7,3).i,Chess(9,5).i);
        isfirst = false;
        next();
    }
    else{
        cal_t = new class std::thread ([this](){
            engine->best_step(cb_with_num.player);
        });
        cal_t->detach();
        wait_cal();
    }
}

void MainWindow::undo(){
    if(undo_save.size() < 1) {
        QMessageBox::information (this,"Infomation","悔棋不合法");
        return;
    }
    QMessageBox msg;
    msg.setText("确认要悔棋吗？");
    msg.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
    int ans = msg.exec();
    if(ans == QMessageBox::Ok){;
        if(undo_save.size() == 1){
            cb_with_num.chessborad[undo_save.top()] = cb_with_num.player;
            if(cb_with_num.player == blackc) cb_with_num.blackcurrentmove = 0;
            else cb_with_num.whitecurrentmove = 0;
            current.time = 0;
            undo_save.pop();
        }
        else if(undo_save.size() == 2){
            int setchess = undo_save.top();
            undo_save.pop();
            int oldchess = undo_save.top();
            undo_save.pop();
            cb_with_num.chessborad[setchess] = nochess;
            cb_with_num.chessborad[oldchess] = cb_with_num.player;
            if(cb_with_num.player == blackc) {
                cb_with_num.blackcurrentmove = 0;
                cb_with_num.blackcurrentset = 0;
                for (int i = 0;i < 4;i++ ) {
                    if(cb_with_num.movechess.blackchess[i].i == setchess){
                        cb_with_num.movechess.blackchess[i] = oldchess;
                    }
                }
            }
            else {
                cb_with_num.whitecurrentmove = 0;
                cb_with_num.whitecurrentset = 0;
                for (int i = 0;i < 4;i++ ) {
                    if(cb_with_num.movechess.whitechess[i].i == setchess){
                        cb_with_num.movechess.whitechess[i] = oldchess;
                    }
                }
            }
            current.time = 0;
        }
        else {
            cb_with_num.player = !cb_with_num.player;
            int hinder = undo_save.top();
            undo_save.pop();
            int setchess = undo_save.top();
            undo_save.pop();
            int oldchess = undo_save.top();
            undo_save.pop();
            cb_with_num.chessborad[setchess] = nochess;
            cb_with_num.chessborad[hinder] = nochess;
            cb_with_num.chessborad[oldchess] = cb_with_num.player;
            if(cb_with_num.player == blackc) {
                cb_with_num.blackcurrentmove = 0;
                cb_with_num.blackcurrentset = 0;
                cb_with_num.blackcurrenthinder = 0;
                for (int i = 0;i < 4;i++ ) {
                    if(cb_with_num.movechess.blackchess[i].i == setchess){
                        cb_with_num.movechess.blackchess[i] = oldchess;
                    }
                }
            }
            else {
                cb_with_num.whitecurrentmove = 0;
                cb_with_num.whitecurrentset = 0;
                cb_with_num.whitecurrenthinder = 0;
                for (int i = 0;i < 4;i++ ) {
                    if(cb_with_num.movechess.whitechess[i].i == setchess){
                        cb_with_num.movechess.whitechess[i] = oldchess;
                    }
                }
            }
            current.time = 0;
        }
    }
    update();
}

void MainWindow::back(){
    if(cb_save.size() <= 1) return;
    while (!undo_save.empty()) {
        undo_save.pop();
    }
    cb_with_num = cb_save[--cur_cb];
    current.time = 0;
    if(cb_with_num.player == computer) isfin = true;
    else isfin = false;
    update();
}

void MainWindow::go(){
    if(cur_cb >= cb_save.size()-1) return;
    while (!undo_save.empty()) {
        undo_save.pop();
    }
    cb_with_num = cb_save[++cur_cb];
    current.time = 0;
    if(cb_with_num.player == computer) isfin = true;
    else isfin = false;
    update();
}
